import AdSearch from '@/components/AdSearch';
import AdSelect from '@/components/AdSelect';
import { queryPerson } from '@/utils/common';

export const codes = {
  select: 'DRIVERPAPERSSELECT',
  add: 'DRIVERPAPERS_ADD',
  edit: 'DRIVERPAPERS_UPDATE',
  showDetail: 'DRIVERPAPERS_VIEW',
  page: 'DRIVERPAPER',
  delete: 'DRIVERPAPERS_DELETE',
  bill: 'DRIVERPAPERS_GENERATEBILL',
};

export const formate = 'YYYY-MM-DD';
export const formateNOSecond = 'YYYY-MM-DD HH:mm';

export const allDispatchType = {
  list: 'driverPaper/selectDriverPaperList',
  detail: 'driverPaper/selectDriverPaperDetail',
  save: 'driverPaper/saveDriverPaper',
  value: 'driverPaper/allValus',
  partsDetail: 'driverPaper/selectPapersDetail',
  setTabsName: 'common/setTabsName',
  queryDriverPaperDict: 'component/queryDriverPaperDict',
  delete: 'driverPaper/delPapers',
  genBills: 'driverPaper/generatedBills',
};

export const routeUrl = {
  add: '/carDaily/driverPaperList/addDriverPaper',
  edit: '/carDaily/driverPaperList/editDriverPaper',
  billEdit: '/carAccount/billList/editBill',
};

export const allUrl = {
  userList: 'tms/tms-papers/selectUserList_s',
  companyList: 'mds/d-customer/selectCompany',
  driverList: 'tms/tms-papers/selectDriverList_s',
  driverDetail: 'tms/tms-driver/viewDetails',
};

export function saveAllValues({ payload = {}, props } = {}) {
  const { dispatch } = props;
  dispatch({
    type: allDispatchType.value,
    payload,
  });
}

export function selectDriverPaperList({ payload = {}, props } = {}) {
  const { dispatch, searchValue } = props;
  dispatch({
    type: allDispatchType.list,
    payload,
    callback: data => {
      if (!data) return;
      queryPerson({ data, props, url: allUrl.userList });
    },
  });
}

export function selectPaperDetail({ type, payload, props, callback }) {
  const { dispatch } = props;
  dispatch({
    type,
    payload,
    callback: data => {
      if (!data) return;
      callback && callback(data);
    },
  });
}

export function renderTableAdSelect({ key, data, value, props }) {
  let params = { onlyRead: true, value };
  if (key) {
    const { dictObject } = props;
    params = { data: dictObject[key], payload: { code: [key] }, ...params };
  } else {
    params = { data, ...params };
  }
  return <AdSelect {...params} />;
}

export function renderTableAdSearch({ value, props }) {
  const { searchValue } = props;
  if (!value || !searchValue) return '';
  const params = {
    onlyRead: true,
    value: searchValue[value],
    label: 'loginName',
    name: 'sysName',
  };
  return <AdSearch {...params} />;
}
